import _ from "lodash";
import moment from "moment";

const comma = (_num, comma) => {
  let num = _num;
  if (num == 0) return "0";
  if (num == "" || _.isUndefined(num) || _.isNull(num)) return "-";
  if (!num && num !== 0) return "-";
  let f = false;
  try {
    num = parseFloat(num);
  } catch (e) {
    return num;
  }
  if (_.isNaN(num)) return num;
  if (comma || comma === 0) {
    num = num.toFixed(comma);
  }
  num = (num || 0).toString();
  if (num.indexOf("-") > -1) {
    num = num.replace("-", "");
    f = true;
  }
  let nums = num.split(".");
  num = nums[0];
  let result = "";
  while (num.length > 3) {
    result = "," + num.slice(-3) + result;
    num = num.slice(0, num.length - 3);
  }
  if (num) {
    result = num + result;
  }
  if (comma || (comma === undefined && nums[1])) {
    result = result + "." + nums[1];
  }
  if (f) {
    result = "-" + result;
  }
  return result;
};
const maxUnitName = number => {
  let u;
  if (number < 1000000000 && number >= 100000) {
    u = Utils.Consts.UNIT_TYPE.TEN_THOUSAND;
  } else if (number >= 1000000000) {
    u = Utils.Consts.BILLION;
  } else {
    u = "";
  }
  return u;
};
class Format {
  constructor() {
    this.comma = comma;
  }

  toDate(date, type) {
    if (!date) return null;
    if (_.isString(date)) {
      switch (type) {
        case "utc":
        case "3":
        case 3:
          return moment(date, moment.ISO_8601).date();
        case "time":
        case 1:
        case "1":
          return moment(date, "HH:mm:ss").date();
        case "daytime":
        case 2:
        case "2":
          return moment(date, "YYYY-MM-DD HH:mm:ss").date();
        case "day":
        case 0:
        case "0":
          return moment(date, "YYYY-MM-DD").date();
        case 4:
        case "4":
          return moment(date, "HH:mm").date();
        default:
          return moment(date, type).date();
      }
    } else {
      return null;
    }
  }

  date(date, type) {
    if (!date) return "-";
    if (date instanceof Date && isNaN(date.getTime())) return "-";
    let d;
    if (date._isAMomentObject) {
      d = _.cloneDeep(date);
    } else {
      // debugger;
      d = moment(date);
    }
    switch (type) {
      case "time":
      case 1:
      case "1":
        return d.format("HH:mm:ss");
      case "daytime":
      case 2:
      case "2":
        return d.format("YYYY-MM-DD HH:mm:ss");
      case "day":
      case 0:
      case "0":
        return d.format("YYYY-MM-DD");
      case 3:
      case "3":
        return d.format("YYYY-MM");
      case 4:
      case "4":
        return d.format("HH:mm");
      default:
        return d.format(type);
    }
  }

  numberMoney(number, type) {
    if (number == 0) return "0";
    if (number == "" || _.isUndefined(number) || _.isNull(number)) return "-";
    let num;
    switch (type) {
      case "sign":
      case 1:
      case "1":
        return "￥" + comma(number);
      case "company":
      case 2:
      case "2":
        num = number / 1000;
        return comma(num) + "万";
      case "thousand":
      case 3:
      case "3":
        num = number / 100;
        return comma(num) + "千";
      case "tenthousand":
      case 4:
      case "4":
        num = number / 10000;
        return comma(num) + "十万";
      case "millionthousand":
      case 5:
      case "5":
        num = number / 100000;
        return comma(num) + "百万";
      case "delimiter":
      case 0:
      case "0":
      default:
        return comma(number);
    }
  }

  numberAmount(number, type) {
    if (!number) return "-";
    let num;
    switch (type) {
      case "company":
      case 2:
      case "2":
        num = number / 1000;
        return comma(num) + "万";
      case "thousand":
      case 3:
      case "3":
        num = number / 100;
        return comma(num) + "千";
      case "tenthousand":
      case 4:
      case "4":
        num = number / 10000;
        return comma(num) + "十万";
      case "millionthousand":
      case 5:
      case "5":
        num = number / 100000;
        return comma(num) + "百万";
      case "delimiter":
      case 0:
      case "0":
      default:
        return comma(number);
    }
  }

  numberPercent(number) {
    if (!number) return "0.00%";
    return comma(number) + "%";
  }

  strIsNull(str) {
    if ((!str && str !== 0 && str !== "0") || str == null || str == undefined) {
      return "-";
    } else {
      return str;
    }
  }

  strIsNull1(str) {
    // debugger;
    if ((!str && str !== 0 && str !== "0") || str == null || str == undefined) {
      return false;
    } else {
      return true;
    }
  }

  numberUnit(_num) {
    let num = _num;
    if (!num && num !== 0)
      return {
        value: "-",
        unit: null
      };
    try {
      num = parseFloat(num);
    } catch (e) {
      return {
        value: "-",
        unit: null
      };
    }
    if (num / 1e8 >= 10) {
      return {
        value: comma(num / 1e8, 2),
        unit: "亿"
      };
    }
    if (num / 1e4 >= 10) {
      return {
        value: comma(num / 1e4, 2),
        unit: "万"
      };
    }
    return {
      value: comma(num),
      unit: null
    };
  }

  // 数字千分位分割符 100,000,000
  numberScience(num) {
    if (typeof num == "number") {
      num = num + "";
    }
    if (!num) return "0.00";
    let value2Array = num.split(".");
    if (value2Array.length == 2) {
      let intPart = Number(value2Array[0]) - (Number(value2Array[0]) % 1);
      let intPartFormat = intPart
        .toString()
        .replace(/(\d)(?=(?:\d{3})+$)/g, "$1,");
      return intPartFormat + "." + value2Array[1];
    } else {
      let intPart = Number(value2Array[0]) - (Number(value2Array[0]) % 1);
      let intPartFormat = intPart
        .toString()
        .replace(/(\d)(?=(?:\d{3})+$)/g, "$1,");
      return intPartFormat;
    }
  }
  // 数据的最大单位转换
  converUnitToText(number, unit = "", unitType) {
    let numberStr, u;
    const ten_thousand = value => {
      return (value / 10000).toFixed(2);
    };
    const billion = value => {
      return (value / 100000000).toFixed(2);
    };
    u = unitType ? unitType : maxUnitName(number);
    numberStr =
      Utils.Consts.UNIT_TYPE.TEN_THOUSAND == u
        ? ten_thousand(number)
        : billion(number);
    return numberStr + u + unit;
  }
  // 获取最大单位的名称
  maxUnitName(number) {
    return maxUnitName(number);
  }
  /**
   * 功能：为数据添加特殊字符（↑、↓、%）
   * */
  addSpecialCharacter(value, comValue, type) {
    if (null != value && undefined != value) {
      if ("01" == type) {
        //添加↑、↓、%
        if (value > comValue) {
          return value + "%" + "↑";
        } else if (value < comValue) {
          return Math.abs(value) + "%" + "↓";
        } else {
          return value + "%";
        }
      } else if ("02" == type) {
        //添加↑、↓
        if (value > comValue) {
          return value + "↑";
        } else if (value < comValue) {
          return Math.abs(value) + "↓";
        } else {
          return value;
        }
      } else if ("03" == type) {
        //添加%
        return value + "%";
      } else {
        return value;
      }
    } else {
      return value;
    }
  }
  /**
   * 功能：为单元格添加样式（↑：cell-num-up、↓：cell-num-down、正常：cell-num）
   * */
  addSpecialCellStyle(value, comValue) {
    if (null != value && undefined != value) {
      if (value > comValue) {
        return "cell-num-up";
      } else if (value < comValue) {
        return "cell-num-down";
      } else {
        return "cell-num";
      }
    }
  }

  /**
   * 格式化日期
   */
  formatDate(ts, format = "YYYY-MM-DD HH:mm:ss") {
    if (typeof ts == "number") {
      ts = ts + "";
    }
    if (typeof ts == "string" && this.strIsNull1(ts)) {
      let date = ts.replace(/\//g, "").replace(/-/g, "");
      if (date.length == 14) {
        date =
          date.substr(0, 4) +
          "-" +
          date.substr(4, 2) +
          "-" +
          date.substr(6, 2) +
          " " +
          date.substr(8, 2) +
          ":" +
          date.substr(10, 2) +
          ":" +
          date.substr(12, 2);
      } else if (date.length == 8) {
        date =
          date.substr(0, 4) + "-" + date.substr(4, 2) + "-" + date.substr(6, 2);
      } else if (date.length == 6) {
        date = date.substr(0, 4) + "-" + date.substr(4, 2);
      }
      return moment(date).format(format);
    }
    return moment(ts).format(format);
  }
}

export default new Format();
